package com.guava;

import com.google.common.base.Optional;

/**
 * OptionalTester--GuavaTest
 *
 * @author: Jovi
 * @createTime: 2018-05-04 16:21
 **/
public class OptionalTester {
    public static void main(String[] args) {
        // NullNormal();

        // NullGuava();

        // NullOptional();

        // testOptional();

    }



    private static void NullGuava() {
        OptionalTester optionalTester1 = new OptionalTester();

        Integer invalidInput = null;
        Optional<Integer> a1 =  Optional.of(invalidInput);
        Optional<Integer> b1 =  Optional.of(new Integer(10));
        System.out.println(optionalTester1.sumGuava(a1,b1));
    }

    private static void NullNormal() {
        OptionalTester optionalTester = new OptionalTester();
        Integer a =  null;
        Integer b =  new Integer(10);
        System.out.println(optionalTester.sumNormal(a,b));
    }

    public Integer sumNormal(Integer a, Integer b){
        return a + b;
    }

    public Integer sumGuava(Optional<Integer> a, Optional<Integer> b){
        return a.get() + b.get();
    }

    private static void NullOptional() {
        OptionalTester optionalTester = new OptionalTester();
        Integer value1 = null;
        Integer value2 = new Integer(10);

        Optional<Integer> a = Optional.fromNullable(value1);
        Optional<Integer> b = Optional.of(value2);

        System.out.println(optionalTester.sumOptional(a,b));

    }

    private Integer sumOptional(Optional<Integer> a, Optional<Integer> b) {

        /* checks the value is present or not */
        System.out.println("First parameter is present:" + a.isPresent());
        System.out.println("Second parameter is present:" + b.isPresent());

        Integer value1 = a.or(new Integer(0));//returns the value if present otherwise return the default value passed.

        Integer value2 = b.get();//gets the value, value should be present

        return value1 + value2;
    }

    private  static void testOptional() {

        Optional<Integer> possible=Optional.of(6);
        Optional<Integer> absentOpt=Optional.absent();
        Optional<Integer> NullableOpt=Optional.fromNullable(null);
        Optional<Integer> NoNullableOpt=Optional.fromNullable(10);
        if(possible.isPresent()){
            System.out.println("possible isPresent:"+possible.isPresent());
            System.out.println("possible value:"+possible.get());
        }
        if(absentOpt.isPresent()){
            System.out.println("absentOpt isPresent:"+absentOpt.isPresent());
        }
        if(NullableOpt.isPresent()){
            System.out.println("fromNullableOpt isPresent:"+NullableOpt.isPresent());
        }
        if(NoNullableOpt.isPresent()){
            System.out.println("NoNullableOpt isPresent:"+NoNullableOpt.isPresent());
        }
    }




}
